/*****************************************************************************
*   adc.h:  Header file for adc.c
*		verr 1.0
******************************************************************************/

#ifndef __ME32G030_ADC_H 
#define __ME32G030_ADC_H

#include "me32g030.h"

//AD channel sel
#define AD0 	0x01
#define AD1 	0x02
#define AD2 	0x04
#define AD3 	0x08
#define AD4 	0x10
#define AD5 	0x20
#define AD6 	0x40
#define AD7 	0x80
#define DIFAD01 	0x01
#define DIFAD23 	0x02
#define DIFAD45 	0x04
#define DIFAD67 	0x08

//DR int
#define ADC0INT 	0x01
#define ADC1INT 	0x02
#define ADC2INT 	0x04
#define ADC3INT 	0x08
#define ADC4INT 	0x10
#define ADC5INT 	0x20
#define ADC6INT 	0x40
#define ADC7INT 	0x80

//DR index
#define ADC_DR0 	0x0
#define ADC_DR1 	0x1
#define ADC_DR2 	0x2
#define ADC_DR3 	0x3
#define ADC_DR4 	0x4
#define ADC_DR5 	0x5
#define ADC_DR6 	0x6
#define ADC_DR7 	0x7
//ADC VREF selcetion
#define INTERNALVDDGND 			0x0
#define EXTVREFPGND 				0x2
#define INTERNALADCVREFGND	0x3

//ADC mode
#define TRIGGERMODE 	0x0
#define BURSTMODE 		0x1

//ADC trigger option
#define ADC_NONE_START 0x0 								//No start (this value should be used when clearing PDN to 0).
#define ADC_START_BY_SOFTWAER 		0x1 		//Start conversion now.
#define ADC_START_BY_CTIM0_CAP0 	0x2 		//Start conversion when the edge selected by bit 24 occurs on CTIM0_CAP0.
#define ADC_START_BY_CTIM1_CAP0 	0x3 		//Start conversion when the edge selected by bit 24 occurs on CTIM1_CAP0.
#define ADC_START_BY_CTIM0_MAT0 	0x4 		//Start conversion when the edge selected by bit 24 occurs on CTIM0_MAT0.
#define ADC_START_BY_CTIM1_MAT0 	0x5 		//Start conversion when the edge selected by bit 24 occurs on CTIM1_MAT0.
#define ADC_START_BY_BTIM0_MAT 		0x6 		//Start conversion when the edge selected by bit 24 occurs on BTIM0_MAT.
#define ADC_START_BY_BTIM1_MAT 		0x7 		//Start conversion when the edge selected by bit 24 occurs on BTIM1_MAT.
#define ADC_START_BY_PWM 					0x8 		//Start conversion when PWM1 cycle Reload,


//ADC trigger signal edge
#define ADC_TRIGGER_RISE_EDGE 	0x0
#define ADC_TRIGGER_FALL_EDGE 	0x1

#define   ADC_IssueSoftTrigger 		ADC->SSCR_b.ADCTRIG = 1
#define   ADC_SelectVref(vref) 		ADC->CR_b.ADCSREF = vref
#define   ADC_ConfigSampleClk(ticks) 		ADC->CR_b.ADCSHT = ticks
#define   ADC_EnableAD01AsDifferentialInput ADC->CR_b.AINS01 = 1
#define   ADC_EnableAD23AsDifferentialInput ADC->CR_b.AINS23 = 1
#define   ADC_EnableAD45AsDifferentialInput ADC->CR_b.AINS45 = 1
#define   ADC_EnableAD67AsDifferentialInput ADC->CR_b.AINS67 = 1
#define   ADC_SelectIntLDOasVrefp 					ADC->CR_b.VREFP_SEL = 1
#define   ADC_SelectEntVDDasVrefp 					ADC->CR_b.VREFP_SEL = 0
#define   ADC_AD0_VALUE					 	ADC->DR0
#define   ADC_AD1_VALUE					 	ADC->DR1
#define   ADC_AD2_VALUE					 	ADC->DR2
#define   ADC_AD3_VALUE					 	ADC->DR3
#define   ADC_AD4_VALUE					 	ADC->DR4
#define   ADC_AD5_VALUE					 	ADC->DR5
#define   ADC_AD6_VALUE					 	ADC->DR6
#define   ADC_AD7_VALUE					 	ADC->DR7

void ADC_Init(uint32_t conversionrate,uint8_t vrefsel);
void ADC_DeInit(void);
void ADC_SetupChannels (uint32_t channelassign, uint32_t triggermode);
void ADC_SetTrigger(uint8_t triggersrc, uint8_t edge);
void ADC_Calibation(void);
void ADC_EnableConversionInt(uint32_t inttype);

#endif /* end __ME32G030_ADC_H */
/*****************************************************************************
**                            End Of File
******************************************************************************/
